package com.ruoyi.web.controller.teaching;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.teaching.domain.*;
import com.ruoyi.teaching.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教务学员Controller
 *
 * @author ruoyi
 * @date 2022-10-11
 */
@Controller
@RequestMapping("/teaching/student")
public class TeachingStudentController extends BaseController {
    private String prefix = "teaching/student";

    @Autowired
    private ITeachingStudentService teachingStudentService;

    @Autowired
    private ITeachingStudentApplyService teachingStudentApplyService;

    @Autowired
    private ITeachingSchoolService teachingSchoolService;

    @Autowired
    private ITeachingCourseService teachingCourseService;

    @Autowired
    private ITeachingClassesStudentsService teachingClassesStudentsService;

    @RequiresPermissions("teaching:student:view")
    @GetMapping()
    public String student() {
        return prefix + "/student";
    }

    /**
     * 查询教务学员列表
     */
//    @RequiresPermissions("teaching:student:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TeachingStudent teachingStudent) {
        startPage();
        teachingStudent.setSchoolId(getSysUser().getSchoolId());
        List<TeachingStudent> list = teachingStudentService.selectTeachingStudentList(teachingStudent);
        for (TeachingStudent student : list){
            Long schoolId = student.getSchoolId();
            student.setTeachingSchool(teachingSchoolService.selectTeachingSchoolBySchoolId(schoolId));
        }
        return getDataTable(list);
    }

    /**
     * 导出教务学员列表
     */
    @RequiresPermissions("teaching:student:export")
    @Log(title = "教务学员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TeachingStudent teachingStudent) {
        List<TeachingStudent> list = teachingStudentService.selectTeachingStudentList(teachingStudent);
        ExcelUtil<TeachingStudent> util = new ExcelUtil<TeachingStudent>(TeachingStudent.class);
        return util.exportExcel(list, "教务学员数据");
    }

    /**
     * 新增教务学员
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存教务学员
     */
    @RequiresPermissions("teaching:student:add")
    @Log(title = "教务学员", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TeachingStudent teachingStudent) {
        teachingStudent.setCreateId(getUserId());
        teachingStudent.setCreateBy(getLoginName());
        teachingStudent.setCreateTime(new Date());
        return toAjax(teachingStudentService.insertTeachingStudent(teachingStudent));
    }

    /**
     * 修改教务学员
     */
    @RequiresPermissions("teaching:student:edit")
    @GetMapping("/edit/{studentId}")
    public String edit(@PathVariable("studentId") Long studentId, ModelMap mmap) {
        TeachingStudent teachingStudent = teachingStudentService.selectTeachingStudentByStudentId(studentId);
        mmap.put("teachingStudent", teachingStudent);
        return prefix + "/edit";
    }

    /**
     * 修改保存教务学员
     */
    @RequiresPermissions("teaching:student:edit")
    @Log(title = "教务学员", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TeachingStudent teachingStudent) {
        teachingStudent.setUpdateBy(getLoginName());
        teachingStudent.setUpdateTime(new Date());
        return toAjax(teachingStudentService.updateTeachingStudent(teachingStudent));
    }

    /**
     * 删除教务学员
     */
    @RequiresPermissions("teaching:student:remove")
    @Log(title = "教务学员", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(teachingStudentService.deleteTeachingStudentByStudentIds(ids));
    }

    /**
     * 学生详情
     */
    @GetMapping("/info/{studentId}")
    public String Info(@PathVariable("studentId") Long studentId, ModelMap mmap) {
        TeachingStudent teachingStudent = teachingStudentService.selectTeachingStudentByStudentId(studentId);
        TeachingStudentApply param = new TeachingStudentApply();
        param.setStudentId(studentId);
//        param.setStatus("0");
        List<TeachingStudentApply> list = teachingStudentApplyService.selectTeachingStudentApplyList(param);
        if (StringUtils.isNotEmpty(list)){
            mmap.put("studentApplys", list);
            for (TeachingStudentApply studentApply: list){
                TeachingSchool school = teachingSchoolService.selectTeachingSchoolBySchoolId(studentApply.getSchoolId());
                TeachingCourse course = teachingCourseService.selectTeachingCourseByCourseId(studentApply.getCourseId());
                mmap.put("teachingCourse", course);
                mmap.put("teachingSchool", school);
            }
        }
        mmap.put("teachingStudent", teachingStudent);
        return prefix + "/info";
    }

    /**
     * 新增学生报课
     */
    @GetMapping("/apply/{studentId}")
    public String apply(@PathVariable("studentId") Long studentId, ModelMap mmap) {
        TeachingStudent teachingStudent = teachingStudentService.selectTeachingStudentByStudentId(studentId);
        mmap.put("teachingStudent", teachingStudent);
        return prefix + "/apply";
    }

    /**
     * 新增保存学生报课
     */
    @RequiresPermissions("teaching:student:apply")
    @Log(title = "学生报课", businessType = BusinessType.INSERT)
    @PostMapping("/apply")
    @ResponseBody
    public AjaxResult addSave(TeachingStudentApply teachingStudentApply) {
        teachingStudentApply.setCreateId(getUserId());
        teachingStudentApply.setCreateBy(getLoginName());
        teachingStudentApply.setCreateTime(new Date());
        Long studentId = teachingStudentApply.getStudentId();
        Long schoolId = teachingStudentApply.getSchoolId();
        Long courseId = teachingStudentApply.getCourseId();
        if (StringUtils.isNull(teachingStudentApply.getLessonCount())) {
            return error("请选择课时");
        }
        TeachingStudentApply param = new TeachingStudentApply();
        param.setStudentId(studentId);
        param.setCourseId(courseId);
        param.setSchoolId(schoolId);
        param.setStatus("0");
        List<TeachingStudentApply> list = teachingStudentApplyService.selectTeachingStudentApplyList(param);
        if (StringUtils.isEmpty(list)) {
            teachingStudentApplyService.insertTeachingStudentApply(teachingStudentApply);
        } else {
            TeachingStudentApply apply = list.get(0);
            int lessonCount = StringUtils.isNull(apply.getLessonCount()) ? 0 : apply.getLessonCount();
            int lessonRemain = StringUtils.isNull(apply.getLessonRemain()) ? 0 : apply.getLessonRemain();
            lessonCount += teachingStudentApply.getLessonCount();
            lessonRemain += teachingStudentApply.getLessonCount();
            apply.setLessonCount(lessonCount);
            apply.setLessonRemain(lessonRemain);
            teachingStudentApplyService.updateTeachingStudentApply(apply);
        }
        return success();
    }

    /**
     * 分配班级列表
     */
    @GetMapping("/allotClass/{applyId}")
    public String allotClass(@PathVariable("applyId") Long applyId, ModelMap mmap){
        TeachingStudentApply studentApply = teachingStudentApplyService.selectTeachingStudentApplyByApplyId(applyId);
//        TeachingClassesStudents param = new TeachingClassesStudents();
//        param.setApplyId(applyId);
//        List<TeachingClassesStudents> list = teachingClassesStudentsService.selectTeachingClassesStudentsList(param);
//        mmap.put("classesStudents", list);
        mmap.put("teachingStudentApply", studentApply);
        return prefix + "/allotClass";
    }
}
